package com.zp.game

import android.app.Activity
import android.content.Context
import com.business.Analyse.report
import com.business.AnalyseKey
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.NetWorkUtil.networkConnected

class PacketDialog(ctx: Context, private val index: Int) : BaseDialog(ctx) {

    private val zpActivity: ZpGame = ctx as ZpGame

    override fun getContentViewResId(): Int = R.layout.dialog_zp_extra

    override fun initView() {
        findBtn(R.id.btnClose).setOnClickListener { cancel() }

        findBtn(R.id.btnPacket).setOnClickListener { btnOpenClicked() }
    }

    private fun btnOpenClicked() {
        if (!networkConnected()) {
            AppUtil.toast("请打开网络!")
            return
        }
        MyLog.elog("看视频....")
        VideoAdUtil.playVideoAd(ctx as Activity) { success: Boolean ->
            if (success) {
                report(AnalyseKey.Zp_Hb_Award)
                if (index in 0..2) {
                    var  awardCount = when (index) {
                        0 -> 188
                        1 -> 288
                        else -> 488
                    }
                    AppUtil.appViewModel().zpExtraAward(index,awardCount)
                    zpActivity.mInfo?.updateAwardState(index, 1)

                    // 通知游戏线程,更新UI
                    zpActivity.director.mainThreadEvent.add { scene ->
                        if (scene  is ZpScene){
                            scene.updateUI(zpActivity.mInfo!!)
                        }
                    }

                    AppUtil.toast(String.format("恭喜获得%d金币奖励!", awardCount))
                    cancel()
                }
            }else{
                AppUtil.toast("视频还未准备好!")
            }
        }
    }

    override fun initData() {
    }

}